package cn.dengta.webapp.user.entity;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

import cn.dengta.common.model.*;
import cn.dengta.common.util.Formats;
import cn.dengta.webapp.base.entity.BizEntity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.EasyDate;
import me.codeplayer.util.StringUtil;

/**
 * 星球探险
 *
 * @author lgy
 * @since 2022-10-13
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName("act_planet_explore")
public class ActPlanetExplore extends BizEntity {

	/** 当期限制投注时间（毫秒） */
	public final static int STOP_BET_TIME = 10000;

	/** 期数 */
	String period;
	/** 参加人数 */
	Integer joinUserCount;
	/** 押注数 */
	Integer betCount;
	/** 钻石数 */
	Integer coin;
	/** 充值金额 */
	BigDecimal recharge;
	/** 中奖星球 */
	Integer prizePlanet;
	/** 中奖人数 */
	Integer prizeUserCount;
	/** 中奖倍数 */
	Integer factor;
	/** 中奖钻石数 */
	Integer prizeCoin;
	/** 开始时间 */
	Date beginTime;
	/** 结束时间 */
	Date endTime;
	/** 规则JSON配置 */
	String config;

	public ActPlanetExplore init(String period, Date beginTime, Date endTime, Date now) {
		this.period = period;
		this.joinUserCount = 0;
		this.betCount = 0;
		this.coin = 0;
		this.recharge = BigDecimal.ZERO;
		this.prizePlanet = null;
		this.factor = 0;
		this.prizeUserCount = 0;
		this.prizeCoin = 0;
		this.beginTime = beginTime;
		this.endTime = endTime;
		this.config = "";
		super.init(Status.NO.value, State.PUBLIC);
		super.initTime(now);
		return this;
	}

	public long timeDiff(Long baseTime) {
		if (baseTime == null) {
			baseTime = System.currentTimeMillis();
		}
		return Math.max(endTime.getTime() - baseTime, 0L);
	}

	public boolean open() {
		return Status.asResult(status);
	}

	public boolean canBuy(Long baseTime) {
		return Status.NO.eq(status) && isActivity(baseTime) && timeDiff(baseTime) > ActPlanetExplore.STOP_BET_TIME;
	}

	public boolean canSettle(Long baseTime) {
		return Status.NO.eq(status) && timeDiff(baseTime) < ActPlanetExplore.STOP_BET_TIME;
	}

	public boolean isActivity(Long baseTime) {
		if (baseTime == null) {
			baseTime = System.currentTimeMillis();
		}
		return SwitchSupport.between(baseTime, beginTime, endTime);
	}

	/** 60分一个周期 */
	public String calcPeriod(Date baseTime) {
		final EasyDate d = new EasyDate(baseTime);
		return Formats.formatDate(baseTime, Formats.formatDate_YMD) + StringUtil.leftPad(String.valueOf((int) (baseTime.getTime() - d.beginOf(Calendar.DAY_OF_MONTH).getTime()) / 3600000), '0', 4);
	}

	public String getBeginTime_() {
		return Formats.formatDefDate(beginTime);
	}

	public static final String PERIOD = "period";
	public static final String JOIN_USER_COUNT = "join_user_count";
	public static final String BET_COUNT = "bet_count";
	public static final String COIN = "coin";
	public static final String RECHARGE = "recharge";
	public static final String PRIZE_PLANET = "prize_planet";
	public static final String PRIZE_USER_COUNT = "prize_user_count";
	public static final String PRIZE_COIN = "prize_coin";
	public static final String BEGIN_TIME = "begin_time";
	public static final String END_TIME = "end_time";
	public static final String CONFIG = "config";

}
